# 作者: 赵若云
# 2025年02月18日22时27分53秒
# 3478783573@qq.com
import my_homework_module


def find_unique_number(num_list):
    """
    1.有7个整数，其中有3个数出现了两次，1个数出现了一次， 找出出现了一次的那个数。
    :param num_list:
    :return:
    """
    unique_number = 0
    for i in range(7):
        unique_number = unique_number ^ num_list[i]
    return unique_number


def use_for():
    """
    2.写一个简单的for循环，从1打印到20，横着打为1排
    :return:
    """
    for i in range(20):
        print(i + 1, end=" ")
    print()


def say_hello(times):
    """
    3.写一个say_hello函数打印多次hello并给该函数加备注（具体打印几次依靠传递的参数），然后调用say_hello，同时学会快速查看函数备注，及如何跳转到函数实现快捷操作（与上课一致）
    :param times:
    :return:
    """
    while (times != 0):
        print('hello')
        times -= 1


# 5.练习列表、元组，字典的各种操作（与上课的代码保持一致）
def use_list():  # 列表，用中括号
    print('-' * 50)
    # 列表生成式
    info_list = [x for x in range(10) if x % 2 == 0]  # 有if放后面
    # a = [j for i in range(10) for j in range(i)] # 两次for用后面的j
    # a = [i if i % 2 == 0 else i ** 2 for i in range(10)] # 有if else就要写前面
    # a = [[col * row for col in range(5)] for row in range(5)] #
    print(info_list)
    print('-' * 50)

    # 查
    print(info_list[0])  # 位置→数据
    print(info_list.index(8))  # 数据→位置

    # 改
    info_list[0] = 1
    print(info_list)
    print('-' * 50)

    # 增
    info_list.append(10)
    print(info_list)

    info_list.insert(0, 0)
    print(info_list)

    info_temp = [9, 9, 9]
    info_list.extend(info_temp)
    print(info_list)
    print('-' * 50)

    # 删
    info_list.remove(1)  # 删除指定数据
    print(info_list)

    info_list.pop(5)  # 删除指定位置的数据
    print(info_list)

    # del info_list[5]
    # info_list.remove(9) # 移除某个数据
    # del info_list[5]
    # print(info_list)

    # info_list.clear()
    # print(info_list)
    print('-' * 50)

    # 统计
    print(len(info_list))  # 长度
    print(info_list.count(9))  # 出现次数
    print('-' * 50)

    # 排序
    info_list.reverse()  # 逆置
    print(info_list)
    info_list.sort()  # 升序
    print(info_list)
    info_list.sort(reverse=True)  # 降序
    print(info_list)
    print('-' * 50)

    # 遍历
    info_list.sort()
    # for
    for num in info_list:
        print(num, end=" ")
    print()
    # while
    i = 0
    while (i < len(info_list)):
        print(info_list[i], end=" ")
        i += 1
    print()
    print('-' * 50)

    # 其他
    a = [1, 2, 3, 4, 5]
    b = [6, 7, 8]
    c = [0] * 10

    print(id(a))
    print(a * 2)  # 输出两次
    print(a + b)
    print(c)  # 十个0

    a += b  # 等价于extend ,和a=a+b是不一样的
    print(a)
    print(id(a))

    a = a + b  # a=a+b是将a重写掉了，地址会变
    print(a)
    print(id(a))
    print('-' * 50)


def use_tuple():  # 元组，只读，用小括号
    info_tuple = ("zhangsan", 18, 1.75, "zhangsan")

    # 查
    print(info_tuple[0])  # 根据位置查值
    print(info_tuple.index(18))  # 根据值找位置

    # 统计
    print(info_tuple.count('zhangsan'))  # 统计某个值出现的次数
    print(len(info_tuple))  # 长度

    # 遍历
    for my_tuple in info_tuple:
        print(my_tuple, end=" ")
    print()
    print('-' * 50)

    # 各种输出赋值操作
    info_tuple = ("小明", 21, 1.85)
    # info_tuple 直接传递给 % 格式化符号
    print("%s的年龄是%d，身高是%.2f" % info_tuple)
    info_string = "%s的年龄是%d，身高是%.2f" % info_tuple
    print(info_string)
    # format() 方法 接受多个参数来替代 {} 占位符。
    info_string = "{}的年龄是{}，身高是{}".format(*info_tuple)
    print(info_string)
    print('-' * 50)


def use_dict():  # 字典，用大括号，实现机制哈希
    xiaoming_dict = {"name": "小明"}
    print(xiaoming_dict)
    print(f'id(xiaoming_dict)={id(xiaoming_dict)}')

    # 查
    print(xiaoming_dict["name"])  # 通过key
    print(xiaoming_dict.get("name123"))  # 安全获取，不报错

    # 增
    xiaoming_dict["age"] = 18
    print(xiaoming_dict)

    # 改
    xiaoming_dict.setdefault("name", "小小明")  # 不覆盖前面的内容，存在不增，不存在新增
    print(xiaoming_dict)
    xiaoming_dict["name"] = "小小明"
    print(xiaoming_dict)
    print(f'id(xiaoming_dict)={id(xiaoming_dict)}')

    # 删
    xiaoming_dict.pop("name")
    print(xiaoming_dict)

    # 统计
    xiaoming_dict = {"name": "小明",
                     "age": 18}
    print(len(xiaoming_dict))  # 长度

    # 合并
    temp_dict = {"height": 1.75,
                 "age": 20}
    xiaoming_dict.update(temp_dict)  # 合并用的update更新，地址也会变
    print(xiaoming_dict)
    print(f'id(xiaoming_dict)={id(xiaoming_dict)}')

    # 清空
    xiaoming_dict.clear()
    print(xiaoming_dict)

    print('-' * 50)

    # 遍历
    xiaoming_dict = {"name": "小明",
                     "qq": "123456",
                     "phone": "10086"}
    # 输出key
    for k in xiaoming_dict:
        print(k)
    print('-' * 50)
    # 输出value
    for v in xiaoming_dict.values():
        print(v)
    print('-' * 50)
    # 输出key+value
    for kv in xiaoming_dict.items():
        print(kv)
    print('-' * 50)
    for k, v in xiaoming_dict.items():
        print(k, v)
    print('-' * 50)

    # list_dict
    card_list = [{"name": "张三",
                  "qq": "12345",
                  "phone": "110"},
                 {"name": "李四",
                  "qq": "54321",
                  "phone": "10086"}
                 ]
    for card in card_list:
        print(card)
    print('-' * 50)

    for card in card_list:
        for k, v in card.items():
            print(k, v)
    print('-' * 50)


# homework1
num_list = [2, 2, 3, 3, 4, 4, 5]
print(find_unique_number(num_list))

# homework2
use_for()

# homework3
times = 7
say_hello(times)  # 按Ctrl点函数跳转

# homework4
if __name__ == '__main__':
    my_homework_module.print_line()  # 调用函数

# homework5
use_list()
use_tuple()
use_dict()
